package com.ruoyi.business.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.ruoyi.business.domain.OperationRemind;
import com.ruoyi.business.domain.WxUser;
import com.ruoyi.business.service.IOperationRemindService;
import com.ruoyi.business.service.IWxUserService;
import com.ruoyi.business.vo.OperationRemindType;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.RecruitJobsMapper;
import com.ruoyi.business.domain.RecruitJobs;
import com.ruoyi.business.service.IRecruitJobsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 招聘岗位Service业务层处理
 * 
 * @author asher
 * @date 2021-11-22
 */
@Service
public class RecruitJobsServiceImpl implements IRecruitJobsService 
{
    @Autowired
    private RecruitJobsMapper recruitJobsMapper;

    @Autowired
    private IWxUserService wxUserService;

    @Autowired
    private IOperationRemindService operationRemindService;

    /**
     * 查询招聘岗位
     * 
     * @param jobId 招聘岗位主键
     * @return 招聘岗位
     */
    @Override
    public RecruitJobs selectRecruitJobsByJobId(Long jobId)
    {
        return recruitJobsMapper.selectRecruitJobsByJobId(jobId);
    }

    /**
     * 查询招聘岗位列表
     * 
     * @param recruitJobs 招聘岗位
     * @return 招聘岗位
     */
    @Override
    public List<RecruitJobs> selectRecruitJobsList(RecruitJobs recruitJobs)
    {
        return recruitJobsMapper.selectRecruitJobsList(recruitJobs);
    }

    /**
     * 新增招聘岗位
     * 
     * @param recruitJobs 招聘岗位
     * @return 结果
     */
    @Override
    @Transactional
    public int insertRecruitJobs(RecruitJobs recruitJobs)
    {
        recruitJobs.setCreateTime(DateUtils.getNowDate());
        int i = recruitJobsMapper.insertRecruitJobs(recruitJobs);
        List<WxUser> wxUsers = wxUserService.selectWxUserList(new WxUser());
        if (wxUsers.isEmpty()) {
            return i;
        }
        List<OperationRemind> list = new ArrayList<>();
        for (WxUser item : wxUsers) {
            list.add(new OperationRemind(OperationRemindType.JOB, "[岗位招聘]您有一条新的招聘信息尚未读取!", recruitJobs.getJobId(), "0", item.getUserId(), null, DateUtils.getNowDate(), recruitJobs.getCreateBy()));
        }
        operationRemindService.batchOperationRemind(list);
        return i;
    }

    /**
     * 修改招聘岗位
     * 
     * @param recruitJobs 招聘岗位
     * @return 结果
     */
    @Override
    public int updateRecruitJobs(RecruitJobs recruitJobs)
    {
        recruitJobs.setUpdateTime(DateUtils.getNowDate());
        return recruitJobsMapper.updateRecruitJobs(recruitJobs);
    }

    /**
     * 批量删除招聘岗位
     * 
     * @param jobIds 需要删除的招聘岗位主键
     * @return 结果
     */
    @Override
    public int deleteRecruitJobsByJobIds(Long[] jobIds)
    {
        ArrayList<Long> integers = new ArrayList<>(Arrays.asList(jobIds));
        for (Long item : integers) {
            operationRemindService.deleteOperationRemindByBusinessId(Long.valueOf(item));
        }
        return recruitJobsMapper.deleteRecruitJobsByJobIds(jobIds);
    }

    /**
     * 删除招聘岗位信息
     * 
     * @param jobId 招聘岗位主键
     * @return 结果
     */
    @Override
    public int deleteRecruitJobsByJobId(Long jobId)
    {
        operationRemindService.deleteOperationRemindByBusinessId(jobId);
        return recruitJobsMapper.deleteRecruitJobsByJobId(jobId);
    }
}
